import { Inject, Optional, Injectable } from "@angular/core";
import { Observer, Observable } from "rxjs";
import { CommonHttpClient } from "services/common-httpclient";
import { map } from "rxjs/operators";
import { PagedResultDto } from "@shared/component-base";
import { DataDictionary, DataDropDown } from "entities";
import { NzTreeNode } from "ng-zorro-antd";

@Injectable()
export class DataDictionaryService {
    private _commonhttp: CommonHttpClient;

    constructor(@Inject(CommonHttpClient) commonhttp: CommonHttpClient) {
        this._commonhttp = commonhttp;
    }
    //获取分页数据
    getAll(params: any): Observable<PagedResultDto> {
        let url_ = "/api/services/app/DataDictionary/GetPagedAsync";
        return this._commonhttp.get(url_, params).pipe(map(data => {
            const result = new PagedResultDto();
            result.items = data.items;
            result.totalCount = data.totalCount;
            return result;
        }));
    }
    /**
 * 获取单条数据
 * @param id 
 */
    getById(id: string): Observable<DataDictionary> {
        let _url = "/api/services/app/DataDictionary/GetByIdAsync";
        let param = { 'id': id };
        return this._commonhttp.get(_url, param).pipe(map(data => {
            return DataDictionary.fromJS(data);
        }));
    }

    /**
     * 更新与创建配置
     * @param input 
     */
    createOrUpdate(input: DataDictionary | null): Observable<DataDictionary> {
        let _url = "/api/services/app/DataDictionary/CreateOrUpdateAsync";
        return this._commonhttp.post(_url, { "DataDictionary": input }).pipe(map(data => {
            return data;
        }))
    }

    //根据group获取字典下拉列表
    getDropDownDtos(group: string): Observable<any> {
        let _url = "/api/services/app/DataDictionary/GetDropDownDtosByGroupAsync";
        return this._commonhttp.get(_url, { group: group }).pipe(map(data => {
            return data;
        }));
    }


    /**
     * 删除配置
     * @param id 
     */
    delete(id: any): Observable<any> {
        let _url = "/api/services/app/DataDictionary/DeleteAsync";
        let param = { 'id': id };
        return this._commonhttp.delete(_url, param);
    }

    getDataTypes(): Observable<any> {
        let _url = "/api/services/app/DataDictionary/GetDataTypesAsync";
        return this._commonhttp.get(_url);
    }

    getDataGroups(groupType: string): Observable<any> {
        let _url = "/api/services/app/DataDictionary/GetDataGroupsAsync";
        let param = { 'groupType': groupType };
        return this._commonhttp.get(_url, param);
    }

    getDataGroupList(groupCode: string): Observable<DataDropDown[]> {
        let _url = "/api/services/app/DataDictionary/GetDataGroupList";
        let param = { 'groupCode': groupCode };
        return this._commonhttp.get(_url, param);
    }

    getDataValue(groupCode: string, code: string, defaultValue: string): Observable<any> {
        let _url = "/api/services/app/DataDictionary/GetDataValue";
        let param = { 'groupCode': groupCode, 'code': code, 'defaultValue': defaultValue };
        return this._commonhttp.get(_url, param);
    }

    getDataGroupNzTree(groupCode: string, selectedCode: string): Observable<NzTreeNode[]> {
        let _url = "/api/services/app/DataDictionary/GetDataGroupNzTree";
        let param = { 'groupCode': groupCode, 'selectedCode': selectedCode };
        return this._commonhttp.get(_url, param);
    }

    //位置
    getlocationpNzTree(): Observable<NzTreeNode[]> {
        let _url = "/api/services/app/Location/GetTreelocation";
        return this._commonhttp.get(_url);
    }

    checkData(groupCode: string, code: string): Observable<boolean> {
        let _url = "/api/services/app/DataDictionary/CheckData";
        let param = { 'groupCode': groupCode, 'code': code };
        return this._commonhttp.get(_url, param);
    }

    treeUpgrade(id: any): Observable<any> {
        let _url = "/api/services/app/DataDictionary/TreeUpgradeAsync";
        let param = { 'id': id };
        return this._commonhttp.post(_url, null, param);
    }

    treeDowngrade(id: any): Observable<any> {
        let _url = "/api/services/app/DataDictionary/TreeDowngradeAsync";
        let param = { 'id': id };
        return this._commonhttp.post(_url, null, param);
    }

    treeTransfer(id: any, parentId: any): Observable<any> {
        let _url = "/api/services/app/DataDictionary/TreeTransferAsync";
        let param = { 'id': id, 'parentId': parentId };
        return this._commonhttp.post(_url, null, param);
    }

}
